/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serwer;

import java.io.ObjectInputStream;

/**
 *
 * @author Marcin
 */
public class NonBlockingInput<T> extends Thread {
    ObjectInputStream ois;
    boolean ready=false;
    boolean empty=true;
    boolean end=false;
    T obj;
    
    
    public NonBlockingInput(ObjectInputStream ois){
        this.ois=ois;
    }
    
     public synchronized boolean ready(){
        return ready;
    }
    
    public T read(){
            T buf=obj;
            empty=true;
            setRdy(false);
            return buf;
    }
    
    public synchronized void setRdy(boolean what){
        ready=what;
    }
    
   
    @Override
    public void run(){
        while(true){
            try {
                while(!empty){Thread.sleep(10);}
                obj=(T)ois.readObject();
                empty=false;
                setRdy(true);
            } catch (Exception e) {
                e.printStackTrace();
                break;                
            }
        }
    }
}
